<?php

namespace App\Console\Commands;

use App\Models\Project;
use App\Models\Team;
use App\Models\User;
use App\Models\Volunteer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class RefreshProject extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'refreshProject';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新项目状态';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        Log::info('refresh project recommend status');

        Project::where('end', '<', date('Y-m-d H;i:s'))->update(['recommend_time'=>null, 'pri'=>10]);

        Project::where('end', '>', date('Y-m-d H;i:s'))->where('pri', 10)->update([ 'pri'=>5]);

        Log::info('refresh team project count');

        $nodes = Project::selectRaw('team_id, count(*) as ct')->groupBy('team_id')->get();

        foreach($nodes as $value)
        {
            Team::where('id', $value->team_id)->update(['project_num'=>$value->ct]);
        }
    }
}
